<template>
  <div>
    <div>
      <el-input
        style="width: 200px; margin-right: 20px"
        size="mini"
        placeholder="策略名称"
        v-model="searchStrategy.name"
        clearable
        @keydown.enter.native="
          searchStrategy.currentPage = 1
          searchStrategy.size = 10
          initStrategy()
        "
      ></el-input>
      <el-button icon="el-icon-delete" type="warning" size="mini" @click="searchStrategy.name = ''">重置</el-button>
      <el-button
        type="success"
        size="mini"
        icon="el-icon-search"
        @click="
          searchStrategy.currentPage = 1
          searchStrategy.size = 10
          initStrategy()
        "
      >
        搜索
      </el-button>
      <el-button type="primary" size="mini" icon="el-icon-plus" @click="showAddConvergenceView">添加策略</el-button>
    </div>

    <div style="margin-top: 10px">
      <el-table
        size="mini"
        :data="strategyData"
        border
        stripe
        :row-class-name="tableRowClassName"
        v-loading="loading"
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.8)"
      >
        <el-table-column label="策略名称" align="center" width="150" prop="name"></el-table-column>
        <el-table-column label="匹配服务" align="center" width="350">
          <template slot-scope="scope">
            <el-tag
              :style="{ marginLeft: marginLeftHandler(index) }"
              type="warning"
              v-for="(item, index) in scope.row.serverNameList"
            >
              {{ item }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="重复告警收敛周期(秒)" align="center">
          <template slot-scope="scope">
            <el-tag type="primary">{{ scope.row.period }} s</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否启用" width="200" align="center">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.enabled"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="changeEnabled(scope.row.id)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="更新时间" align="center" width="200" prop="updateTime"></el-table-column>
        <el-table-column label="操作" fixed="right" width="100">
          <template slot-scope="scope">
            <el-button size="mini" style="padding: 3px" type="primary" @click="showUpdateConvergenceView(scope.row)">
              编辑
            </el-button>
            <el-button size="mini" style="padding: 3px" type="danger" @click="deleteConvergence(scope.row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div style="display: flex; justify-content: flex-end; margin-top: 5px">
      <el-pagination
        background
        layout="sizes, prev, pager, next, jumper, ->, total, slot"
        :current-page="searchStrategy.currentPage"
        :page-size="searchStrategy.size"
        :total="total"
        @current-change="currentChange"
        @size-change="sizeChange"
      ></el-pagination>
    </div>

    <el-dialog
      :close-on-click-modal="false"
      :title="title"
      @close="closeDialog()"
      :visible.sync="dialogVisible"
      width="600px"
    >
      <el-form ref="convergenceForm" :model="convergence" :rules="rules">
        <table>
          <tr>
            <el-form-item label="策略名称" label-width="200px" prop="name">
              <el-input
                size="mini"
                style="width: 200px"
                v-model="convergence.name"
                placeholder="请输入策略名称"
              ></el-input>
            </el-form-item>
          </tr>
          <tr>
            <el-form-item label="匹配服务" label-width="200px" prop="serverIdList">
              <el-select
                v-model="convergence.serverIdList"
                size="mini"
                multiple
                style="width: 200px"
                placeholder="匹配服务"
                clearable
                filterable
              >
                <el-option v-for="item in servers" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </tr>
          <tr>
            <el-form-item label="重复告警收敛周期(秒)" label-width="200px" prop="period">
              <el-input-number size="mini" style="width: 150px" v-model="convergence.period"></el-input-number>
            </el-form-item>
          </tr>
          <tr>
            <el-form-item label-width="200px" label="是否启用">
              <el-switch
                v-model="convergence.enabled"
                active-color="#13ce66"
                inactive-color="#ff4949"
                style="padding-bottom: 6px"
              ></el-switch>
            </el-form-item>
          </tr>
        </table>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="doConvergence">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { serverGetAll } from '@/api/alarm/server'
import { getStrategyByPage, addStrategy, updateStrategy, deleteStrategy, strategyEnabled } from '@/api/alarm/convergence'

export default {
  name: 'Convergence',
  data() {
    return {
      servers: [],
      searchStrategy: {
        name: '',
        currentPage: 1,
        size: 10
      },
      strategyData: [],
      loading: false,
      total: 0,
      title: '',
      dialogVisible: false,
      convergence: {
        id: null,
        name: '',
        period: null,
        serverIdList: null,
        enabled: true
      },
      rules: {
        name: [{ required: true, message: '请输入策略名称', trigger: 'blur' }],
				serverIdList: [{ required: true, message: '请选择匹配服务', trigger: 'change' }],
        period: [{ required: true, message: '请输入重复告警收敛周期', trigger: 'blur' }]
      }
    }
  },
  computed: {},
  mounted() {
    this.initServer()
    this.initStrategy()
  },
  methods: {
    marginLeftHandler(index) {
      // 除了第一个元素 后面都 margin-left 5px
      if (index !== 0) {
        return '5px'
      }
    },
    initStrategy() {
      this.loading = true
      getStrategyByPage(this.searchStrategy).then((resp) => {
        this.loading = false
        if (resp) {
          this.strategyData = resp.data
          this.total = resp.total
        }
      })
    },
    currentChange(page) {
      this.searchStrategy.currentPage = page
      this.initStrategy()
    },
    sizeChange(size) {
      this.searchStrategy.size = size
      this.initStrategy()
    },
    initServer() {
      serverGetAll().then((resp) => {
        if (resp) {
          this.servers = resp
        }
      })
    },
    showAddConvergenceView() {
      this.convergence = {
        id: null,
        name: '',
        period: null,
        serverIdList: null,
        enabled: true
      }
      this.dialogVisible = true
      this.title = '添加策略'
    },
    showUpdateConvergenceView(data) {
      // 拷贝
      this.convergence = JSON.parse(JSON.stringify(data))
      this.dialogVisible = true
      this.title = '编辑策略'
    },
    closeDialog() {
      this.$refs['convergenceForm'].resetFields()
    },
    changeEnabled(id) {
      strategyEnabled({ id: id });
    },
    doConvergence() {
      if (this.convergence.id) {
        // 编辑
        this.$refs['convergenceForm'].validate((valid) => {
          if (valid) {
            updateStrategy(this.convergence).then((resp) => {
              if (resp) {
                this.dialogVisible = false
                this.initStrategy()
              }
            })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      } else {
        // 新增
        this.$refs['convergenceForm'].validate((valid) => {
          if (valid) {
            addStrategy(this.convergence).then((resp) => {
              if (resp) {
                this.dialogVisible = false
                this.initStrategy()
              }
            })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      }
    }
  }
}
</script>

<style>
.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #f0f9eb;
}

label {
  display: inline-block;
  width: 100px;
  text-align: right;
}
</style>
